package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.ClaimWechatWithdrawalBo;
import org.dromara.system.domain.bo.SysUserWithdrawalBo;
import org.dromara.system.domain.bo.WithdrawalApprovalBo;
import org.dromara.system.domain.vo.SysUserWithdrawalVo;
import org.dromara.system.domain.vo.WeChatTransferToChangeVo;
import org.dromara.system.service.ISysUserWithdrawalService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 提现记录
 *
 * @author Lion Li
 * @date 2025-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userWithdrawal")
public class SysUserWithdrawalController extends BaseController {

    private final ISysUserWithdrawalService sysUserWithdrawalService;

    /**
     * 查询提现记录列表
     */
//    @SaCheckPermission("system:userWithdrawal:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserWithdrawalVo> list(SysUserWithdrawalBo bo, PageQuery pageQuery) {
        return sysUserWithdrawalService.queryPageList(bo, pageQuery);
    }

    //前台查询提现记录
    @GetMapping("/listReception")
    public TableDataInfo<SysUserWithdrawalVo> listReception(SysUserWithdrawalBo bo, PageQuery pageQuery) {
        return sysUserWithdrawalService.listReception(bo, pageQuery);
    }

    /**
     * 导出提现记录列表
     */
//    @SaCheckPermission("system:userWithdrawal:export")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserWithdrawalBo bo, HttpServletResponse response) {
        List<SysUserWithdrawalVo> list = sysUserWithdrawalService.queryList(bo);
        ExcelUtil.exportExcel(list, "提现记录", SysUserWithdrawalVo.class, response);
    }

    /**
     * 获取提现记录详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:userWithdrawal:query")
    @GetMapping("/{id}")
    public R<SysUserWithdrawalVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(sysUserWithdrawalService.queryById(id));
    }

    /**
     * 新增提现记录
     */
//    @SaCheckPermission("system:userWithdrawal:add")
    @Log(title = "发起提现 需审核", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/withdrawalInitiation")
    public R<Void> withdrawalInitiation(@Validated(AddGroup.class) @RequestBody SysUserWithdrawalBo bo) {
        return toAjax(sysUserWithdrawalService.insertByBo(bo));
    }

    @Log(title = "微信提现无需审核", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/wechatWithdrawalInitiation")
    public R<Boolean> wechatWithdrawalInitiation(@Validated(AddGroup.class) @RequestBody SysUserWithdrawalBo bo) {
        return R.ok("提现发起成功，请在提现记录中领取！", sysUserWithdrawalService.wechatWithdrawalInitiation(bo));
    }

    //微信支付回调
    @PostMapping("/wechatWithdrawalInitiation/callBack")
    @SaIgnore
    public String wechatWithdrawalInitiationCall(@RequestBody HashMap<String, Object> map) {
        return sysUserWithdrawalService.wechatWithdrawalInitiationCall(map);
    }


    @Log(title = "提现审批", businessType = BusinessType.OTHER)
    @RepeatSubmit(interval = 8, timeUnit = TimeUnit.SECONDS, message = "点太快啦，稍后再试！")
    @PostMapping("/withdrawalApproval")
    public R<Void> withdrawalApproval(@RequestBody @Validated WithdrawalApprovalBo bo) {
        return toAjax(sysUserWithdrawalService.withdrawalApproval(bo));
    }

    @Log(title = "领取提现", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/claimWithdrawal")
    public R<WeChatTransferToChangeVo> claimWechatWithdrawal(@Validated @RequestBody ClaimWechatWithdrawalBo bo) {
        return R.ok(sysUserWithdrawalService.claimWechatWithdrawal(bo));
    }

    /**
     * 修改提现记录
     */
//    @SaCheckPermission("system:userWithdrawal:edit")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserWithdrawalBo bo) {
        return toAjax(sysUserWithdrawalService.updateByBo(bo));
    }

    /**
     * 删除提现记录
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:userWithdrawal:remove")
    @Log(title = "提现记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysUserWithdrawalService.deleteWithValidByIds(List.of(ids), true));
    }


    @GetMapping("/queryWithdrawalStatusEnums")
    public R<List<HashMap<String, Object>>> queryWithdrawalStatusEnums() {
        return R.ok(sysUserWithdrawalService.queryWithdrawalStatusEnums());
    }

    @GetMapping("/withdrawalPostingReview")
    public TableDataInfo<SysUserWithdrawalVo> withdrawalPostingReview(PageQuery pageQuery) {
        return sysUserWithdrawalService.withdrawalPostingReview(pageQuery);
    }


    @Log(title = "执行 待确认到账提现 退款", businessType = BusinessType.OTHER)
    @PostMapping("/withdrawalRefund")
    @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点执行
    public void withdrawalRefund() {
        sysUserWithdrawalService.withdrawalRefund();
    }



}
